﻿// DensitySlicingAttributeDialog.cpp: 实现文件
//

#include "pch.h"
#include "DensitySlicingAttributeDialog.h"
#include "afxdialogex.h"
#include"resource.h"
#include"iostream"
#include<string>
#include<vector>
#include <fstream>
#include <sstream>
using namespace std;
/*全局变量区域*/
vector<CString> FilePathvector;
vector<int> FenjiArray;
int R[256], G[256], B[256];
CImageList m_ListImageList;
int SePuZhuCount = 0;
int max = 0;
int min = 255;
int xiaohuishu = 0;
//extern CImage readImage;
CImage result;
CImage MidImage;
CImage temp[256];     //最多分256
extern CImage readImage;
CImageList imagelist;

vector<CImage> test;

int shuaxinkaiguan = 0;
int ButttonClickCount = 0;



// DensitySlicingAttributeDialog 对话框

IMPLEMENT_DYNAMIC(DensitySlicingAttributeDialog, CDialogEx)

DensitySlicingAttributeDialog::DensitySlicingAttributeDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_DSAttribute, pParent)
{

}

DensitySlicingAttributeDialog::~DensitySlicingAttributeDialog()
{
}

void DensitySlicingAttributeDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, DensityChart, m_List);
	DDX_Control(pDX, IDC_LIST_SePuZhu, m_ListCtrl);
	DDX_Control(pDX, IDC_COMBO_BandChoice, m_cbselect);
}


BEGIN_MESSAGE_MAP(DensitySlicingAttributeDialog, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_Show, &DensitySlicingAttributeDialog::OnBnClickedButtonShow)
	ON_BN_CLICKED(IDC_BUTTON_PutColor, &DensitySlicingAttributeDialog::OnBnClickedButtonPutcolor)
	ON_WM_PAINT()
	ON_CBN_SELCHANGE(IDC_COMBO_BandChoice, &DensitySlicingAttributeDialog::OnCbnSelchangeComboBandchoice)
END_MESSAGE_MAP()


// DensitySlicingAttributeDialog 消息处理程序


BOOL DensitySlicingAttributeDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	CRect rect;

	// 获取编程语言列表视图控件的位置和大小   
	m_List.GetClientRect(&rect);

	// 为列表视图控件添加全行选中和栅格风格   
	m_List.SetExtendedStyle(m_List.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

	// 为列表视图控件添加三列   
	m_List.InsertColumn(0, _T("颜色"), LVCFMT_CENTER, rect.Width() / 4, 0);
	m_List.InsertColumn(1, _T("下限"), LVCFMT_CENTER, rect.Width() / 4, 1);
	m_List.InsertColumn(2, _T("上限"), LVCFMT_CENTER, rect.Width() / 4, 2);
	m_List.InsertColumn(3, _T("序号"), LVCFMT_CENTER, rect.Width() / 4, 3);

	SetDlgItemText(IDC_EDIT_live, _T("5"));

	if(!m_ListImageList)
		m_ListImageList.Create(256, 22, 24, 2, 2);//
 
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void DensitySlicingAttributeDialog::OnBnClickedButtonShow()
{
	// TODO: 在此添加控件通知处理程序代码

	if (ButttonClickCount != 0)
	{
		if (readImage)
		{
			ButttonClickCount = 0;
			int nIndex = m_cbselect.GetCurSel();

			DensitySlicing(nIndex);
		}
		else
		{
			AfxMessageBox(_T("未读入图片或分级未选择！！！"));
		}
	}
	else if (ButttonClickCount == 0)
	{
		AfxMessageBox(_T("该参数已显示，请重新选择！！！"));
		return;
	}
}


void DensitySlicingAttributeDialog::OnBnClickedButtonPutcolor()
{
	// TODO: 在此添加控件通知处理程序代码


	BOOL isOpen = TRUE;		//是否打开(否则为保存)
	CString defaultDir = L"D:";	//默认打开的文件路径
	CString fileName = L"";			//默认打开的文件名
	CString filter = L"文件 (*.lib;)||";	//文件过虑的类型

	CFileDialog openFileDlg(isOpen, defaultDir, fileName, OFN_HIDEREADONLY | OFN_READONLY, filter, NULL);
	INT_PTR result = openFileDlg.DoModal();
	CString FilePath;
	CString Filename;
	if (result == IDOK) {
		FilePath = openFileDlg.GetPathName();
		Filename = openFileDlg.GetFileName();

	}

	ReadColor(FilePath);//读入色谱库
	

	//创建色谱柱
	CImage linshi;
	linshi.Create(256, 22, 24);

	byte* pRealData;
	pRealData = (byte*)linshi.GetBits();
	int pit = linshi.GetPitch();
	int bitCount = linshi.GetBPP() / 8;

	for (int y = 0; y < linshi.GetHeight(); y++)
	{
		for (int x = 0; x < linshi.GetWidth(); x++)
		{
			
			
			*(pRealData + pit * y + x * bitCount) = B[x];
			*(pRealData + pit * y + x * bitCount + 1) = G[x];
			*(pRealData + pit * y + x * bitCount + 2) = R[x];

		}

	}

	CStatic* pic = (CStatic*)GetDlgItem(IDC_SingleChromatoGraphic);//图片控件显示读入的照片
	pic->SetBitmap(HBITMAP(linshi));

	HBITMAP hBitmap = linshi.Detach(); // 获得位图句柄 用以转换
	CBitmap bmp;
	bmp.DeleteObject();
	bmp.Attach(hBitmap); //  转换为CBitmap对象
	m_ListImageList.Add(&bmp, RGB(0, 0, 0));

	m_ListCtrl.SetImageList(&m_ListImageList, LVSIL_SMALL);
	m_ListCtrl.InsertItem(SePuZhuCount, Filename, SePuZhuCount);
	FilePathvector.push_back(FilePath);
	SePuZhuCount++;

	//Invalidate();
}


void DensitySlicingAttributeDialog::OnPaint()
{
	CPaintDC dc(this); // device context for painting
}

void DensitySlicingAttributeDialog::ReadColor(CString path)
{
	vector<string> lines;//读取文本文件
	ifstream file;

	string buf;
	file.open(path, ios::in);
	if (file.is_open())
		while (getline(file, buf))
		{
			lines.push_back(buf);

		}
	file.close();

	for (int i = 1; i <= 256; i++)
	{
		string str = lines[i];
		string str1[4];
		istringstream is(str);
		is >> str1[0] >> str1[1] >> str1[2] >> str1[3];

		R[i - 1] = atof((str1[1].c_str()));
		G[i - 1] = atof((str1[2].c_str()));
		B[i - 1] = atof((str1[3].c_str()));

	}
}

void DensitySlicingAttributeDialog::GetMinMax(int flags)
{
	
	BYTE rgb[3] = { 0 }, avg = 0;
	int x, y;
	//地址操作，效率高
	byte* pRealData;
	pRealData = (byte*)readImage.GetBits();
	int pit = readImage.GetPitch();
	int bitCount = readImage.GetBPP() / 8;

	int Comperzhi = 0;
	

	for (int y = 0; y < readImage.GetHeight(); y++)
	{
		for (int x = 0; x < readImage.GetWidth(); x++)
		{

			if (flags == 1)
			{
				Comperzhi = *(pRealData + pit * y + x * bitCount);

			}
			else if (flags == 2)
			{
				Comperzhi = *(pRealData + pit * y + x * bitCount + 1);
			}
			else if (flags == 3)
			{
				Comperzhi = *(pRealData + pit * y + x * bitCount + 2);
			}
			else if (flags == 4)
			{
				Comperzhi = (int)(int)(*(pRealData + pit * y + x * bitCount))*0.3
					+ (int)(int)(*(pRealData + pit * y + x * bitCount + 1))*0.59
					+ (int)(int)(*(pRealData + pit * y + x * bitCount + 2))*0.11;
			}

			if (max < Comperzhi)
			{
				max = Comperzhi;

			}
			if (min > Comperzhi)
			{
				min = Comperzhi;
			}
		}
	}
}

void DensitySlicingAttributeDialog::OnCbnSelchangeComboBandchoice()
{
	if (m_ListCtrl.GetSelectionMark() == -1)
	{
		AfxMessageBox(_T("请选择色谱柱！！！"));
		return;
	}
	// TODO: 在此添加控件通知处理程序代码
	CString strfenji; 
	ButttonClickCount = 1;
	GetDlgItem(IDC_EDIT_live)->GetWindowText(strfenji);
	int fenji = _ttoi(strfenji);
	if (fenji < 1)
	{
		AfxMessageBox(_T("请选择分类数！！！"));
		return;
	}
	if (shuaxinkaiguan!=0)
	{
		m_List.DeleteAllItems();
		//m_ListImageList.DeleteImageList();
		imagelist.DeleteImageList();

		for (int i = 0; i < xiaohuishu; i++)
		{
			temp[i].Destroy();
		}
		FenjiArray.clear();
	}

	MidImage.Destroy();

	//抛出警告

	if (readImage&&fenji>0 && m_ListCtrl.GetSelectionMark() != -1)
	{
		int nIndex = m_cbselect.GetCurSel() + 1;
		GetMinMax(nIndex);
		
		//显示参数
		CString strmin, strmax;
		strmin.Format(_T("%d"), min);
		strmax.Format(_T("%d"), max);
		GetDlgItem(IDC_EDIT_min)->SetWindowText(strmin);
		GetDlgItem(IDC_EDIT_max)->SetWindowText(strmax);

		int i = m_ListCtrl.GetSelectionMark();//确定色谱柱

		CString path = FilePathvector[i];   
		ReadColor(path);//读取色谱柱
		DivisionColorarry(fenji);//色谱柱分级

		//创建图片显示比力尺度
		
		imagelist.Create(100, 30, 24, 2, 2);
		
		for (int i = 0; i < fenji; i++)
		{
			temp[i].Create(100, 30, 24);
			byte* pRealData;
			pRealData = (byte*)temp[i].GetBits();
			int pit = temp[i].GetPitch();
			int bitCount = temp[i].GetBPP() / 8;

			for (int y = 0; y < temp[i].GetHeight(); y++)
			{
				for (int x = 0; x < temp[i].GetWidth(); x++)
				{
					int a = FenjiArray[i];
					*(pRealData + pit * y + x * bitCount) =B[FenjiArray[i]];
					*(pRealData + pit * y + x * bitCount + 1) = G[FenjiArray[i]];
					*(pRealData + pit * y + x * bitCount + 2) = R[FenjiArray[i]];

				}
			}
			//temp[i].Save(_T("./test.jpg"));
			HBITMAP hBitmap = temp[i].Detach(); // 获得位图句柄 用以转换
			CBitmap bmp;
			bmp.DeleteObject();
			bmp.Attach(hBitmap);   //转换为CBitmap对象
			imagelist.Add(&bmp, RGB(0, 0, 0));
		}
		
		m_List.SetImageList(&imagelist, LVSIL_SMALL);

		// 在列表视图控件中插入列表项，并设置列表子项文本   
		
		int xiaxian = 0;
		int shangxian = 256 / fenji;

		for (int  ii = 0; ii < fenji; ii++)
		{
			m_List.InsertItem(ii, _T(" "), ii);

			CString  strshangxian, strxiaxian,strxuhao;
			strshangxian.Format(_T("%d"), shangxian);
			strxiaxian.Format(_T("%d"), xiaxian);
			strxuhao.Format(_T("%d"), ii + 1);
			m_List.SetItemText(ii, 1, strxiaxian);
			m_List.SetItemText(ii, 2, strshangxian);
			m_List.SetItemText(ii, 3, strxuhao);
			
			xiaxian = xiaxian + 256 / fenji;
			shangxian=shangxian+ 256 / fenji;
		}

		xiaohuishu = fenji;
	}
	else
	{
		AfxMessageBox(_T("未读入图片或分级未选择！！！"));
	}
}

void DensitySlicingAttributeDialog::DivisionColorarry(int n)
{
	for (int i = 0; i < n; i++)
	{
		int linshi = int((256 / (2 * n)) + (256 / n)*i);   //    2weizhi
		FenjiArray.push_back(linshi);
		int Rvalue = R[linshi];
		int Gvalue = G[linshi];
		int Bvalue = B[linshi];

		int qishi = (256 / n) * i;
		int zhongzhi = (256 / n) + (256 / n) *i;

		for (int j = qishi; j < zhongzhi; j++)
		{
			R[j] = Rvalue;
			G[j] = Gvalue;
			B[j] = Bvalue;

		}
	}
}

void DensitySlicingAttributeDialog::DensitySlicing(int flags)
{
	//地址操作，效率高
	
	MidImage.Create(readImage.GetWidth(), readImage.GetHeight(), 24);

	byte* pRealData;
	pRealData = (byte*)readImage.GetBits();

	byte* midimagedata;
	midimagedata = (byte*)MidImage.GetBits();


	int pit = readImage.GetPitch();
	int bitCount = readImage.GetBPP() / 8;

	//建立对应的灰度图
	for (int y = 0; y < readImage.GetHeight(); y++)
	{
		for (int x = 0; x < readImage.GetWidth(); x++)
		{
			int grayVal;

			if (flags==0)
			{
				grayVal =*(pRealData + pit * y + x * bitCount);
			}
			else if (flags==1)
			{
				grayVal = *(pRealData + pit * y + x * bitCount + 1);
			}
			else if (flags==2)
			{
				grayVal = *(pRealData + pit * y + x * bitCount + 2);
			}
			else if (flags==3)
			{
				 grayVal = (int)(int)(*(pRealData + pit * y + x * bitCount))*0.3
					+ (int)(int)(*(pRealData + pit * y + x * bitCount + 1))*0.59
					+ (int)(int)(*(pRealData + pit * y + x * bitCount + 2))*0.11;
			}
			*(midimagedata + pit * y + x * bitCount) = B[grayVal];
			*(midimagedata + pit * y + x * bitCount + 1) = G[grayVal];
			*(midimagedata + pit * y + x * bitCount + 2) = R[grayVal];
		}

	}

	CWnd* pWin = AfxGetApp()->GetMainWnd();//获取主对话框指针，不能用以名称获取对话框指针的方式；
	//CDC *pDc = pWin->GetDC();//通过对话框指针，获取对话框的画布
	//HDC  hDC = pDc->GetSafeHdc();

	pWin->Invalidate(FALSE);


	//readImage.Draw(hDC, 100, 100, 250, 350);

	shuaxinkaiguan = 1;
}
